<?php

namespace App\Http\Controllers;

use App\Models\Node;
use App\Models\Role;
use App\Models\RoleNode;
use Illuminate\Http\Request;

class RoleController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $name = $request->get('name');
        $data = Role::when($name, function ($query) use ($name) {
            $query->where('name', 'like', '%' . $name . '%');
        })->paginate($this->pageSize);
        return view('admin.role.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.role.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $params = $request->validate([
            'name' => 'required|unique:roles|min:1|max:6',
        ]);
        $res = Role::create($params);
        if ($res) {
            return redirect(route("admin.role.index"));
        } else {
            return redirect(route("admin.role.create"))->withErrors("添加失败");
        }
    }



    public function edit(Request $request,Role $role)
    {
        $id = $request->get('id');
        $data = Role::find($id);
        return view('admin.role.edit', compact('role','data'));
    }


    public function update(Request $request)
    {
        //dd($request->post());
        $id = $request->get('id');
        $params = $request->validate([
            'name' => 'required',
        ]);
        //dd($params);
        $res = Role::where('id',$id)->update($params);
        //dd($id);
        if ($res) {
            return redirect(route("admin.role.index"));
        } else {
            return redirect(route("admin.role.create"))->withErrors("修改失败");
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //删除数据
        $res = Role::destroy($id);

        //删除成功和失败都要返回提示信息
        if ($res) {
            return $this->writeJson(0, [], '删除角色成功');
        } else {
            return $this->writeJson(20001, [], '删除角色失败');
        }
    }

    /**
     * 给角色分配权限页面
     * @return void
     */
    public function node(Role $role)
    {
        //所有的权限列表
        $data = (new Node())->getNodeAll();
        //当前角色所拥有的权限展示
        //$role->nodes() 相当于当前角色去role_nodes中查询得到所有的权限
        //pluck('node_id'） 把结果集进行一个提取，只需要node_id的数据作为一个新的一维数组
        $roleNode = $role->nodes()->pluck('node_id')->toArray();
        //需要传递当前角色对象
        return view('admin.role.node', compact('data', 'role','roleNode'));
    }

    /**
     * 角色分配权限的方法
     */
    public function nodeSave(Role $role, Request $request)
    {
        $params = $request->get('node');
        $res = $role->nodes()->sync($params);
        //写入成功，跳转到管理员列表页面
        if ($res) {
            return redirect(route("admin.role.index"));
        } else {
            //写入失败，还在当前页，来一个友好提示
            return redirect(route("admin.role.index"))->withErrors("分配权限失败");
        }
    }
}
